package org.cdc.divination.frame.parents;

import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.cdc.divination.frame.annotation.Data;
import org.cdc.divination.frame.annotation.Enable;
import org.cdc.divination.frame.data.DataPool;
import org.cdc.divination.frame.register.Register;

import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class AbstractPlugin extends JavaPlugin {
    protected List<String> scanPackage = new ArrayList<>();
    private Configuration config;
    @Override
    public void onLoad(){
        config = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("divination.yml")));
    }
    @Override
    public void onEnable() {
        initPool();
        if (config.getBoolean("scanpackage.enable")){

        } else {
            registerClass(new Register(this));
        }
        Arrays.stream(this.getClass().getMethods()).forEach(a->{
            if (a.isAnnotationPresent(Enable.class)){
                try {
                    a.invoke(this,Register.getParams(a.getParameters()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 如果关闭了扫包模式就会调用
     * @param register 注册器
     */
    protected void registerClass(Register register){}
    public void scanPackage(){

    }
    private void initPool(){
        DataPool.addPublicData("pluginInstance",this,true);
        DataPool.addPublicData("logger",this.getLogger(),true);
        DataPool.addPublicData("config",this.getConfig(),true);
        DataPool.addPublicData("pluginPath",this.getDataFolder(),true);
        DataPool.addPublicData("pluginName",this.getName(),true);
        DataPool.addPublicData("file",this.getFile(),true);
        DataPool.addPublicData("server",this.getServer(),true);
    }

}
